app.controller('bpmManagerProcessinstanceController', ['$scope', 'httpWrapService', 'dialogs', '$state', '$stateParams','GridFactory',
        function($scope, httpWrapService, dialogs, $state, $stateParams,GridFactory) {

    var columnDefs = [
        {name: "流程实例ID", field: "id",
            cellTemplate: '<a ui-sref="app.bpm.manager.process.trace.view({executionId:{{COL_FIELD}} })">{{COL_FIELD}}</a>'
        },
        {name: "流程定义ID", field: "processDefinitionId"} ,
        {name: "流程名称", field: "processDefinitionId", cellFilter : 'bpmManagerProcessDefinitionFilter : "name"'} ,
        {name: "流程版本", field: "processDefinitionId", cellFilter : 'bpmManagerProcessDefinitionFilter : "version"'} ,
        {name: "业务KEY", field: "businessKey"} ,
        {name: "状态", field: "suspended", cellFilter: 'dictionaryFiter : "suspended"'} ,
        {
            displayName: "",
            field: "suspended",
            enableFiltering: false,
            enableSorting: false,
            maxWidth: 90,
            cellTemplate: '<button class="btn btn-default m-b-xs w-xs" ng-click="grid.appScope.changeState(this)">{{COL_FIELD ? \'激活\' : \'挂起\'}}</button>'
        },
        {
            displayName: "",
            field: "id",
            enableFiltering: false,
            enableSorting: false,
            maxWidth: 90,
            cellTemplate: '<button class="btn btn-danger m-b-xs w-xs" ng-disabled="grid.appScope.Grid.deletes[\'{{COL_FIELD}}\']" ng-click="grid.appScope.delete(this)">删除</button>'
        }
    ];

    $scope.Grid = GridFactory.newInstance({
        url : "bpm/processinstance/list.do",
        columnDefs : columnDefs,
        gridOptions : {
            useExternalSorting : false,
            useExternalPagination : true,
            useExternalFiltering : false
        }
    });
    $scope.Grid.load();


    $scope.changeState = function(target) {
        var id = target.$parent.row.entity.id;
        var state = target.$parent.row.entity.suspended ?  'active' : 'suspended';
        httpWrapService.defaultPost('bpm/processinstance/'+ state + '/' + id +'.do').then(function(){
            $scope.Grid.load();
        });
    };


    $scope.delete = function(target) {
        var id = target.$parent.row.entity.id;
        var dlg = dialogs.create('tpl-common\/inputDialog.html','inputDialogCtrl',{title : '请输入删除原因'},{size:'md',keyboard: true,backdrop: true,windowClass: 'my-class'});
        dlg.result.then(function(deleteReason){
            httpWrapService.post('bpm/processinstance/delete/'+ id +'.do', {
                deleteReason : deleteReason
            }).then(function(){
                $scope.Grid.deleteRow(target.$parent.row.entity);
            }, function(){
                dialogs.notify('错误', '删除失败', {size : 'sm'});
            });
        },function(){
        });
    };
}]);

/**
 */
app.filter('bpmManagerProcessDefinitionFilter', ["$sessionStorage", "httpWrapService","customObjectFilterFactory", function($sessionStorage, httpWrapService, customObjectFilterFactory) {
    return function(input, filed){
        return customObjectFilterFactory.newInstance({
            param : {},
            getUrl : function(intput, filed){
                return 'bpm/processinstance/view/' +input+ '.do';
            },
            getKey : function(intput, filed){
                return 'bpmManagerProcessDefinitionFilter_' + input;
            }
        }).filter(input, filed);
    };
}]);
